package com.bw.medical.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bw.medical.entity.DiagnoseEntity;
import com.bw.medical.entity.DockerLevelEntity;
import com.bw.medical.service.DiagnoseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.bw.medical.entity.PatientEntity;
import com.bw.medical.service.PatientService;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.R;



/**
 * 患者表 
 *
 * @author wj
 * @email sunlightcs@gmail.com
 * @date 2021-08-22 21:39:10
 */
@RestController
@RequestMapping("medical/tbpatient")
public class PatientController {
    @Autowired
    private PatientService patientService;
    @Autowired
    private DiagnoseService diagnoseService;

    /**
     * 患者管理列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        IPage<DockerLevelEntity> page = patientService.getLists(params);
        PageUtils pageUtils = new PageUtils(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getPages());
        return R.ok().put("page", pageUtils);
    }

    @PostMapping("/login")
    public R login(@RequestBody PatientEntity patientEntity){
        PatientEntity patientEntities=patientService.login(patientEntity);
        if (patientEntities!=null){
            return R.ok().put("user",patientEntities);
        }
        return R.error(500,"密码或用户名错误");
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{patientId}")
    public R info(@PathVariable("patientId") Integer patientId){
        PatientEntity patient = patientService.getById(patientId);

        return R.ok().put("patient", patient);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PatientEntity patient){
		patientService.save(patient);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PatientEntity patient){
		patientService.updateById(patient);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] patientIds){
		patientService.removeByIds(Arrays.asList(patientIds));

        return R.ok();
    }

    @RequestMapping("/upd")
    @Transactional
    public R upd(@RequestBody PatientEntity PatientEntity){
        System.out.println(PatientEntity);
        patientService.updateById(PatientEntity);
        DiagnoseEntity tbDiagnoseEntity = new DiagnoseEntity();
        BeanUtils.copyProperties(PatientEntity,tbDiagnoseEntity);
        tbDiagnoseEntity.setPatientId(PatientEntity.getPatientId());//id
        tbDiagnoseEntity.setReportAge(PatientEntity.getPatientAge());//年龄
        tbDiagnoseEntity.setReportWeight(PatientEntity.getPatientWeight());//体重
        tbDiagnoseEntity.setReportHight(PatientEntity.getPatientHight());//身高
        tbDiagnoseEntity.setReportMarital(PatientEntity.getPatientMarital());//结婚
        tbDiagnoseEntity.setReportShen(PatientEntity.getPatientShen());//肾功能
        tbDiagnoseEntity.setReportGan(PatientEntity.getPatientGan());//肝功能
        tbDiagnoseEntity.setReportFertility(PatientEntity.getPatientFertility());//生育状态
        tbDiagnoseEntity.setReportBeer(PatientEntity.getPatientWine());//酒
        boolean save = diagnoseService.save(tbDiagnoseEntity);
        return R.ok();
    }

}
